/**
 * 班组角色管理
 */
Ext.define('Module.jc.JcBzjs', {
	extend : 'Ext.panel.Panel',
	alias : 'widget.jcBzjsPanel',
	layout: 'border',
//	requires: ['Module.jc.TreeCombox'],
	config:{
		bzId:'',
		bzName:''
	},
	initComponent : function() {
		var me = this;
		var store=Ext.create('Ext.data.Store',{
			autoLoad: false,
			fields: ['id','bzjsCode','bzjsName','upuser','uptime','name','describe','jcBzId','sbid','bzName','sbName',
			         'bdzName','cjzName','code','ms'],
	        proxy: {
	            type: 'ajax',
	            url: 'jc/JcBzjsAction/getJcBzjs.do'
	        }
		});
		
		var menutree =me.menutree=  Ext.create('Ext.tree.Panel',{
			title:"班组角色",
			rootVisible: false,
			border: true,
//			singleExpand: true,
			region: 'west',
			width: 240,
			id:me.id+"menutree",
			expanded: true, 
            trackMouseOver:true,
			store: Ext.create('Ext.data.TreeStore', {
				fields: ['id','leaf','text','pid'],
		        proxy: {
		            type: 'ajax',
		            url: 'jc/JcBzjsAction/getBdzBzTree.do'
		        },
		        root: {
		        	expanded: true,  
		            id: '0'
		        }
		    }),
		    tbar:[{  
		        xtype: 'trigger',  
		        triggerCls: 'x-form-clear-trigger',  
		        onTriggerClick: function () {  
		            this.setValue('');  
		            treeFilter(menutree,'');
		        },  
		        width:'100%',  
		        emptyText:'请输入班组名称',  
		        enableKeyEvents: true,  
		        listeners: {  
		            keyup: {  
		                fn: function (field, e) {  
		                	var v=field.getValue();
		                	treeFilter(menutree,v);
		                }  
		            }  
		        }   
		    }],   
			listeners:{
				select:function(r,c){
					if(typeof(c.raw.pid) !='undefined' ){
						me.setBzId(c.raw.pid);
						me.setBzName(c.raw.text);
						store.proxy.extraParams = {
								jcBzId:c.raw.pid
						};
						store.load();
					}
					
					
				}
			}
		});
		
		
		var jcBzjsPanel = me.jcBzjsPanel = Ext.create('Ext.grid.Panel',{
			title: '班组角色信息列表',
			region: 'center',
			padding: '0 1 0 0',
			store: store,
			tbar:[{
				xtype: 'panel',
				border: false,
				height: 25,
				width: '100%',
				bodyPadding: 0-1,
				layout: 'column',
				defaults: {columnWidth: 1,bodyPadding: 0},
				items: [{
						xtype: 'toolbar',
						items: [{
							text: '添加信息',
							iconCls: 'icon-add',
							handler: function(){
								var rec = me.menutree.getSelectionModel().getSelection()[0];
								if(rec && rec.data.leaf !=''){
									me.addJcBzjs(rec);
									Ext.getCmp(me.id+'bzName').setValue(rec.data.text);
									Ext.getCmp(me.id+"jcBzId").setValue(me.getBzId());
								}
								else{
									Ext.Msg.alert('提示', '请选择要添加的班组数据！');
								}
							}
						},{
							text: '修改信息',
							iconCls: 'icon-edit',
							handler: function(){
								var rec = jcBzjsPanel.getSelectionModel().getSelection()[0];
								if(rec){
									rec.data.text = me.menutree.getSelectionModel().getSelection()[0].data.text;
									me.updateJcBzjs(rec);
								}
								else{
									Ext.Msg.alert('提示', '请选择要修改的信息！');
								}
							}
						},{
							text: '删除信息',
							iconCls: 'icon-delete',
							handler: function(){
								var rec = jcBzjsPanel.getSelectionModel().getSelection()[0];
								if(rec){
									me.delJcBzjs(rec);
								}
								else{
									Ext.Msg.alert('提示', '请选择要删除的信息！');
								}
							}
						},{
							xtype:"textfield",
							fieldLabel : '班组角色名称',	
							maxLength:40,
							labelWidth:85,
							width:190
						},{
							xtype:"textfield",
							fieldLabel : '班组角色编码',	
							maxLength:20,
							labelWidth:85,
							width:190
						},{
							text:"查询",
							iconCls: 'icon-search',
							handler: function(){
								var tbar = this.up('toolbar');
								var items = tbar.items.items;
								var rec = me.menutree.getSelectionModel().getSelection()[0];
								console.log(rec.data.pid);
								if(rec && rec.data.leaf !=''){
//								if(me.getBzId() !=''){
									jcBzjsPanel.store.proxy.extraParams = {
											bzjsName: items[3].getSubmitValue(),
											bzjsCode: items[4].getSubmitValue(),
											jcBzId: rec.data.pid//me.getBzId()
									};
									jcBzjsPanel.store.load();
								}
							}
						}
					]
				 }]
			}],
			columns: [{
	            text: '班组角色名称',
	            flex: 2,
	            dataIndex: 'bzjsName'
			},{
	            text: '班组角色编码',
	            flex: 1,
	            dataIndex: 'bzjsCode'
			},{
	            text: '设备ID',
	            flex: 1,
	            dataIndex: 'sbid'
			},{
	            text: '更新用户',
	            flex: 2,
	            dataIndex: 'name'
			},{
	            text: '更新时间',
	            flex: 2,
	            dataIndex: 'uptime',
	            renderer: formatDate
			}]
		});
		
		
		me.items = [menutree,jcBzjsPanel];
		
		me.callParent();
	},
	updateJcBzjs: function(rec){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 410,
			height: 265,
			iconCls: 'icon-edit',
			modal: true,
			layout: 'fit',
			title: '修改信息',
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('panel').items.items[0];
        	    	var form = formp.getForm();
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'jc/JcBzjsAction/updateJcBzjs.do',
            	    	    success: function(form, action) {
            	    	    	var rs = Ext.decode(action.response.responseText);
            	    	    	if(rs.rst){
            	    	    		Ext.Msg.alert('提示', '修改成功！');
            	    	    		formp.up('window').close();
            	    	    		me.jcBzjsPanel.store.load({
            	    	    			params:{
            	    	    				jcBzId:rec.data.jcBzId
            	    	    			}
            	    	    		});
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', '修改失败！详情：'+rs.msg);
            	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '无效的信息,请正确填写信息！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: me.getForm({
				bodyPadding: 20,
				border: false,
				listeners: {
					afterrender: function(fp){
						fp.loadRecord(rec||{});
					}
				}
			})
		}).show();
	},
	addJcBzjs: function(rec){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 410,
			height: 280,
			iconCls: 'icon-add',
			modal: true,
			layout: 'fit',
			title: '添加信息',
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('panel').items.items[0];
        	    	var form = formp.getForm();
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'jc/JcBzjsAction/addJcBzjs.do',
            	    	    success: function(form, action) {
            	    	    	var rs = Ext.decode(action.response.responseText);
            	    	    	if(rs.rst){
            	    	    		Ext.Msg.alert('提示', '保存成功！');
            	    	    		formp.up('window').close();
            	    	    		me.jcBzjsPanel.store.load({
            	    	    			params:{
            	    	    				jcBzId:me.getBzId()
            	    	    			}
            	    	    			
            	    	    		});
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', '保存失败！详情：'+rs.msg);
            	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '无效的信息,请正确填写信息！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: me.getForm({
				bodyPadding: 20,
				border: false,
				listeners: {
					afterrender: function(fp){
						fp.loadRecord(rec||{});
					}
				}
			})
		}).show();
	},
	delJcBzjs: function(rec){
		var me = this;
		if(rec){
			Ext.Msg.confirm('提示', '你确定要删除当前信息吗？',function(b){
				if('yes' == b){
					Ext.Ajax.request({
					    url: 'jc/JcBzjsAction/deleteJcBzjs.do',
					    params: rec.data,
					    success: function(response){
        	    	    	var rs = Ext.decode(response.responseText);
        	    	    	if(rs.rst){
        	    	    		Ext.Msg.alert('提示', '删除成功！');
        	    	    		me.jcBzjsPanel.store.load({
        	    	    			params:{
        	    	    				jcBzId:rec.data.jcBzId,
        	    	    				bzjsName:rec.data.jcBzId.bzjsName
        	    	    			}
        	    	    		});
        	    	    	}
        	    	    	else{
        	    	    		Ext.Msg.alert('提示', '删除失败！详情：'+rs.msg);
        	    	    	}
					    }
					});
				}
			});
		}
		else{
			Ext.Msg.alert('提示', '请选择要删除的信息！');
		}
	},
	getForm: function(config,readOnly){
		var me=this;
		return Ext.apply({
			xtype: 'form',
			bodyPadding: 10,
			defaults: {anchor: '100%'},
			items: [{
					name: 'id',
					xtype: 'hidden'
				},{
					name: 'jcBzId',
					xtype: 'hidden',
					id:me.id+"jcBzId"
				}/*,{
					name: 'sbid',
					xtype: 'hidden',
					id:me.id+'zcglid'
				}*/,{
					fieldLabel: '班组名称',
					name: 'bzName',
					readOnly:true,
					id:me.id+'bzName',
					maxLength:40,
					xtype: 'textfield',
					allowBlank: false
				},{
					fieldLabel: '班组角色名称',
					name: 'bzjsName',
					maxLength:20,
					xtype: 'textfield',
					allowBlank: false
				},{
					fieldLabel: '班组角色编码',
					name: 'bzjsCode',
					xtype: 'textfield',
					maxLength:40,
					allowBlank: false
				},{
					fieldLabel: '设备ID',
					xtype: 'textfield',
					name: 'sbid',
//					readOnly:true,
					maxLength:40,
					allowBlank: true
//					hidden: true,
				},/*{
					xtype : "treecombox",
					name : "name",
					fieldLabel : "终端设备",
					displayField : "name",
					valueField : "sbid",
//					forceSelection : true,
//					autoSelect : true,
					rootVisible:false,
			        maxPickerHeight:160,  
					store : Ext.create('Ext.data.TreeStore', {
						fields: ['id','leaf','name','code','ms','pid','sbid','isLeaf','sjid','sbid'],
				        proxy: {
				            type: 'ajax',
				            url: 'zcglAction/getZcglTree.do'
				        },
				        root: {
				        	expanded: true,  
				            id: '0'
				        }
				    })
				},*/{
					xtype: 'textarea',
					fieldLabel: '描述',
					name: 'describe',
					maxLength:150
				}
			]
		},config);
	},
	getShowForm: function(config,readOnly){
		return Ext.apply({
			xtype: 'form',
			bodyPadding: 10,
			defaults: {anchor: '100%',xtype: 'displayfield'},
			items: [{
					fieldLabel: '变电站名称',
					name: 'bdzName',
				},{
					fieldLabel: '采集站名称',
					name: 'cjzName',
				},{
					fieldLabel: '设备名称',
					name: 'sbName'
				},{
					fieldLabel: '班组角色编码',
					name: 'code'
				},{
					fieldLabel: '描述',
					name: 'ms',
				}
			]
		},config);
	},
	showTreeWin :function (){/*
		var me=this;
		var win= Ext.create("Ext.window.Window",{
				layout:'border',
				width:300,
				height:400,
				iconCls:'icon-cog_edit',
				modal:true,
				title:'设备资产',
				buttons:[{
					text:'保存',
					handler:function(){
						var rec = Ext.getCmp(me.id+'sbTree').getSelectionModel().getSelection()[0];
						if(rec && rec.data.sbid !=''){
							Ext.getCmp(me.id+'sbName').setValue(rec.data.name);
							Ext.getCmp(me.id+'zcglid').setValue(rec.data.sbid);
							win.close();
						}
						else{
							Ext.Msg.alert('提示', '请选择设备信息！');
						}
					}
				},{
					text:'关闭',
					handler:function(){
						win.close();
					}
				}],
			items:[{
				xtype:'treepanel',
				rootVisible: false,
				border: true,
				singleExpand: true,
				region: 'center',
				id:me.id+'sbTree',
				expanded: true, 
				store:  Ext.create('Ext.data.TreeStore', {
					fields: ['id','leaf','name','sbid'],
			        proxy: {
			            type: 'ajax',
			            url: 'zcglAction/getZcglTree.do'
			        },
			        root: {
			        	expanded: true,  
			            id: '0'
			        }
			    }),
			    columns: [{
					xtype: 'treecolumn', 
		            text: '设备名称',
		            width:280,
		            sortable: false,
		            dataIndex: 'name'
				}]
			}]	
		}).show();;
	*/}
});